<template>
    <h1>添加借款款项</h1>

    <table>
        <tr>
            <td>借款名称</td>
            <td>
                <input type="text" v-model="Borrow.BM_Name" >
            </td>
        </tr>

        <tr>
            <td>借款最高年限</td>
            <td>
                <input type="text" v-model="Borrow.BM_Year" >
            </td>
        </tr>

        <tr>
            <td>借款最高额度</td>
            <td>
                <input type="text" v-model="Borrow.BM_Money" >
            </td>
        </tr>

        <tr>
            <td>借款介绍</td>
            <td>
                <textarea v-model="Borrow.BM_Info" >

                </textarea>
            </td>
        </tr>

        <tr>
            <td>借款利息</td>
            <td>
                <select v-model="Borrow.BMI_Id" >
                    <option value="0">--请选择--</option>
                    <option v-for="item in Interest" :key="item.BMI_Id" :value="item.BMI_Id">
                        {{ item.BMI_Year }}年{{ item.BMI_Interest }}%
                    </option>
                </select>
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="添加" @click="AddBorrow" >
            </td>
        </tr>


    </table>

</template>

<script setup lang="ts">

import { ref,onMounted } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'

const router = useRouter();

const Borrow = ref({
    BM_Name:'',
    BM_Year:'',
    BM_Money:'',
    BM_Info:'',
    BMI_Id:''
})

const Interest = ref([{
    BMI_Id:1,
    BMI_Year:'',
    BMI_Interest:''
}])

onMounted(()=>{
    SaveInvest();
})

const SaveInvest = ()=>{
    axios({
        method:'get',
        url:'http://localhost:56707/api/Borrow/SaveInvest',
    })
    .then(function(res){
        console.log(res.data);
        Interest.value = res.data;
    })
    .catch(function(err){
        console.log(err);
    })
}

const AddBorrow = ()=>{
    axios({
        method:'post',
        url:'http://localhost:56707/api/Borrow/AddBorrow',
        data:{
            BM_Name:Borrow.value.BM_Name,
            BM_Year:Borrow.value.BM_Year,
            BM_Money:Borrow.value.BM_Money,
            BM_Info:Borrow.value.BM_Info,
            BMI_Id:Borrow.value.BMI_Id
        }
    })
    .then(res=>{
        if(res.data>0){
            alert('添加成功');
            router.push('/ShowBorrow');
        }
        else{
            alert('添加失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}


</script>
